import { Component, OnInit, AfterViewInit, OnDestroy } from '@angular/core';
import { Device, FormatDate } from 'app/classes/Device';
import { DeviceService } from 'app/share/device.service';
import { Subscription } from 'rxjs/Rx';
import { Http } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { environment } from 'environments/environment';
import { EmitData } from 'app/time-type/time-type.component';

@Component({
  selector: 'app-indicate-query',
  templateUrl: './indicate-query.component.html',
  styleUrls: ['./indicate-query.component.css']
})
export class IndicateQueryComponent implements OnInit, AfterViewInit, OnDestroy  {
  ths = ['所属企业名', '采集名', '倍率', '费率', '起始示值', '结束示值', '分摊比例(%)', '电量(kWh)']
  total = 29998;
  subscription: Subscription;
  type = '10086'
  _params = {
    'meterID': '',
    'begindate': new Date().toLocaleDateString(),
    'enddate': new Date().toLocaleDateString(),
    'datatype': this.type
  }
  begindate: object;
  enddate: object;
  deviceInfo: Device;
  observable: Observable<any>;
  url = environment.httpUrl;
  constructor(
    public deviceService: DeviceService,
    public http: Http,
  ) { }

  ngOnInit() {
    // 初始化设备信息
    this.deviceInfo = this.deviceService.getDeviceInfo();

    // 初始化时间
    const date = new Date();
    this.enddate = {
      formatted: new FormatDate(date).formatToString()
    }
    this.begindate = {
      formatted: new FormatDate(date).formatToString(10)
    }
  }
  ngAfterViewInit(): void {
    this.subscription = this.deviceService.getMessage().subscribe(msg => {
      this.deviceInfo = msg;
      this._params.meterID = this.deviceInfo.meterID;
      this.observable = this.http.get(this.url, { params: this._params }).map( res => res.json() );
      this.observable.subscribe(
        data => {
        },
        err => {
          console.log(err);
          alert('后台没有返回数据');
        }
      );
    })
  }
  ngOnDestroy(): void {
    this.subscription.unsubscribe();
  }
  getDate(e: EmitData) {

  }
}
